package com.jlz.methods;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jlz.actions.login.UserLoginChecker;
import com.julewa.Authority;
import com.julewa.Client;

@Component
public class AuthorityChecker implements Authority{
	private static final String HOME = "global_home";
//	private static final String LOGIN_USER = "global_login_user";
	private static final String LOGIN_NOTICE = "global_user_renotice";
	private static final String LOGIN_CONSOLE = "global_loginas_admin";
	private static final String NS_USER = "/user";
	private static final String NS_CONSOLE = "/console";
	private static final String NS_REGISTER = "/register";
	private @Autowired Client client;
	private @Autowired UserLoginChecker loginChecker;
	@Override
	public String getUnauthorizedResult(String namespace, String actionname,
			Class<?> actionclass, String actionmethod) {
		//auto login
		UserLoginChecker.STATE state = NS_REGISTER.equals(namespace)?null:loginChecker.check();
		if(state == UserLoginChecker.STATE.NOTICE) return LOGIN_NOTICE;
		//System.out.println(namespace + "," + actionname + "," + actionmethod);
		if(namespace.startsWith(NS_USER)){
			if(client.isAnonimous() || client.getUser().getType() < 0){//manager type
				if(state != UserLoginChecker.STATE.ERROR){
					client.setError("j_login", "请您先登陆吧~");
				}
				return HOME;
			}
		}
		if(namespace.startsWith(NS_CONSOLE)){
			if(client.isAnonimous() || client.getUser().getType() > 0){
				return LOGIN_CONSOLE;
			}
		}
		return null;
	}
	
}
